#include "MyFreertos.h"
#include "user.h"
#include "mvb.h"

__attribute__((section("Version")))struct image_version cur_ver =
{
		.iv_major = 1,
		.iv_minor = 11,
		.iv_revision = 0x4c4c,
		.iv_build_num = 0x4c4c4c4c
};

#define MainTask_PRIO      3
#define MainTask_STK_SIZE  512
TaskHandle_t            MainTask_Handler;
void MainTask(void *pvParameters);

#define Can1SendTask_PRIO      3
#define Can1SendTask_STK_SIZE  512
TaskHandle_t            Can1SendTask_Handler;
void Can1SendTask(void *pvParameters);

#define Can2SendTask_PRIO      3
#define Can2SendTask_STK_SIZE  512
TaskHandle_t            Can2SendTask_Handler;
void Can2SendTask(void *pvParameters);

#define MvbSendTask_PRIO     3
#define MvbSendTask_STK_SIZE  512
TaskHandle_t            MvbSendTask_Handler;
void MvbSendTask(void *pvParameters);

#define MvbRecvTask_PRIO     3
#define MvbRecvTask_STK_SIZE  512
TaskHandle_t            MvbRecvTask_Handler;
void MvbRecvTask(void *pvParameters);

#define SdCardTask_PRIO      3
#define SdCardTask_STK_SIZE  512
TaskHandle_t            SdCardTask_Handler;
void SdCardTask(void *pvParameters);

#define FtpTask_PRIO      3
#define FtpTask_STK_SIZE  1024
TaskHandle_t            FtpTask_Handler;
void FtpTask(void *pvParameters);

#define EthQosRecvTask_PRIO      3
#define EthQosRecvTask_STK_SIZE  512
TaskHandle_t            EthQosRecvTask_Handler;
void EthQosRecvTask(void *pvParameters);

#define EthQosSendTask_PRIO      3
#define EthQosSendTask_STK_SIZE  512
TaskHandle_t            EthQosSendTask_Handler;
void EthQosSendTask(void *pvParameters);

#define LedTask_PRIO      2
#define LedTask_STK_SIZE  128
TaskHandle_t            LedTask_Handler;
void LedTask(void *pvParameters);

EventGroupHandle_t can_recv_event_handle;
SemaphoreHandle_t file_mutex_handle, record_data_mutex_handle;
QueueHandle_t can1_recv_queue_ebv11_a, can1_recv_queue_2A, can1_recv_queue_eiu31_a, can1_recv_queue_eiu32_a;
QueueHandle_t can1_recv_queue_eiu33_a, can1_recv_queue_eiu3a_a, can1_recv_queue_eiu3_a, can1_recv_queue_eiu4_a;
QueueHandle_t can1_recv_queue_eiu5_a, can1_recv_queue_eiu6_a, can1_recv_queue_bpcm61_a, can1_recv_queue_bpcm62_a;
QueueHandle_t can1_recv_queue_bpcm63_a, can1_recv_queue_bccm71_a, can1_recv_queue_bccm72_a, can1_recv_queue_bccm73_a;
QueueHandle_t can1_recv_queue_bccm74_a, can1_recv_queue_ibcm81_a, can1_recv_queue_ibcm82_a;
QueueHandle_t can1_send_queue_91, can1_send_queue_92;

QueueHandle_t can2_recv_queue_ebv11_b, can2_recv_queue_2B, can2_recv_queue_eiu31_b, can2_recv_queue_eiu32_b;
QueueHandle_t can2_recv_queue_eiu33_b, can2_recv_queue_eiu3a_b, can2_recv_queue_eiu3_b, can2_recv_queue_eiu4_b;
QueueHandle_t can2_recv_queue_eiu5_b, can2_recv_queue_eiu6_b, can2_recv_queue_bpcm61_b, can2_recv_queue_bpcm62_b;
QueueHandle_t can2_recv_queue_bpcm63_b, can2_recv_queue_bccm71_b, can2_recv_queue_bccm72_b, can2_recv_queue_bccm73_b;
QueueHandle_t can2_recv_queue_bccm74_b, can2_recv_queue_ibcm81_b, can2_recv_queue_ibcm82_b;
QueueHandle_t can2_send_queue_91, can2_send_queue_92;

QueueHandle_t mvb_recv_queue_421, mvb_recv_queue_021, mvb_recv_queue_703, mvb_recv_queue_707;
QueueHandle_t mvb_send_queue_137, mvb_send_queue_702, mvb_send_queue_706;

uint8_t enet_qos_recv_buff[1024];
int enet_qos_sock;
struct sockaddr_in enet_qos_cli;
struct ip_mreq enet_qos_mreq;
extern ip4_addr_t enet_qos_netif_ipaddr;

/**20230613_begin*****modified********************************************************/
uint16_t recordWord[256];
uint16_t record_buff[256];
/**20230613_end*******modified******************************************************/
uint8_t *pCANID31;
uint8_t *pCANID32;
uint8_t *pCANID33;
uint8_t *pCANID34;
uint8_t *pCANID3A;
uint8_t *pCANID61;
uint8_t *pCANID62;
uint8_t *pCANID63;
uint8_t *pCANID64;
uint8_t *pCANID71;
uint8_t *pCANID72;
uint8_t *pCANID73;
uint8_t *pCANID74;
uint8_t *pCANID81;
uint8_t *pCANID82;
uint8_t *pCANID83;
uint8_t *pCANID1A;
uint8_t *pCANID7A;
uint8_t *pCANID91;
uint8_t *pCANID92;
uint16_t *pMVB137;
uint16_t *pMVB421;
uint16_t *pMVB21;
uint16_t *pMVB703;
uint16_t *pMVB707;
uint16_t *pMVB702;
uint16_t *pMVB706;
/**20230613_begin*************************************************************/
uint8_t *pYearMonth;
uint8_t *pDayHour;
uint8_t *pMinuteSeconds;
uint32_t *pMicroS;
snvs_lp_srtc_datetime_t rtc_date_now;
void RtcInit()
{
	snvs_lp_srtc_datetime_t srtcDate;
	snvs_lp_srtc_config_t snvsSrtcConfig;
	SNVS_LP_SRTC_GetDefaultConfig(&snvsSrtcConfig);
	SNVS_LP_SRTC_Init(SNVS, &snvsSrtcConfig);
	SNVS_LP_SRTC_GetDatetime(SNVS, &srtcDate);
	if(srtcDate.year<2020)
	{
		/* Set a start date time and start RT */
		srtcDate.year   = 2025U;
		srtcDate.month  = 07U;
		srtcDate.day    = 2U;
		srtcDate.hour   = 11U;
		srtcDate.minute = 55;
		srtcDate.second = 0;
//		printf("set time\n");
		/* Set SRTC time to default time and date and start the SRTC */
		SNVS_LP_SRTC_SetDatetime(SNVS, &srtcDate);
	}
	SNVS_LP_SRTC_StartTimer(SNVS);
}
void QueueCreat()
{
	can1_recv_queue_ebv11_a = xQueueCreate(1, sizeof(uint8_t [8]));
	can1_recv_queue_2A = xQueueCreate(1, sizeof(uint8_t [8]));
	can1_recv_queue_eiu31_a = xQueueCreate(1, sizeof(uint8_t [8]));
	can1_recv_queue_eiu32_a = xQueueCreate(1, sizeof(uint8_t [8]));
	can1_recv_queue_eiu33_a = xQueueCreate(1, sizeof(uint8_t [8]));
	can1_recv_queue_eiu3a_a = xQueueCreate(1, sizeof(uint8_t [8]));
	can1_recv_queue_eiu3_a = xQueueCreate(1, sizeof(uint8_t [8]));
	can1_recv_queue_eiu4_a = xQueueCreate(1, sizeof(uint8_t [8]));
	can1_recv_queue_eiu5_a = xQueueCreate(1, sizeof(uint8_t [8]));
	can1_recv_queue_eiu6_a = xQueueCreate(1, sizeof(uint8_t [8]));
	can1_recv_queue_bpcm61_a = xQueueCreate(1, sizeof(uint8_t [8]));
	can1_recv_queue_bpcm62_a = xQueueCreate(1, sizeof(uint8_t [8]));
	can1_recv_queue_bpcm63_a = xQueueCreate(1, sizeof(uint8_t [8]));
	can1_recv_queue_bccm71_a = xQueueCreate(1, sizeof(uint8_t [8]));
	can1_recv_queue_bccm72_a = xQueueCreate(1, sizeof(uint8_t [8]));
	can1_recv_queue_bccm73_a = xQueueCreate(1, sizeof(uint8_t [8]));
	can1_recv_queue_bccm74_a = xQueueCreate(1, sizeof(uint8_t [8]));
	can1_recv_queue_ibcm81_a = xQueueCreate(1, sizeof(uint8_t [8]));
	can1_recv_queue_ibcm82_a = xQueueCreate(1, sizeof(uint8_t [8]));
	can1_send_queue_91 = xQueueCreate(1, sizeof(uint8_t [8]));
	can1_send_queue_92 = xQueueCreate(1, sizeof(uint8_t [8]));

	can2_recv_queue_ebv11_b = xQueueCreate(1, sizeof(uint8_t [8]));
	can2_recv_queue_2B = xQueueCreate(1, sizeof(uint8_t [8]));
	can2_recv_queue_eiu31_b = xQueueCreate(1, sizeof(uint8_t [8]));
	can2_recv_queue_eiu32_b = xQueueCreate(1, sizeof(uint8_t [8]));
	can2_recv_queue_eiu33_b = xQueueCreate(1, sizeof(uint8_t [8]));
	can2_recv_queue_eiu3a_b = xQueueCreate(1, sizeof(uint8_t [8]));
	can2_recv_queue_eiu3_b = xQueueCreate(1, sizeof(uint8_t [8]));
	can2_recv_queue_eiu4_b = xQueueCreate(1, sizeof(uint8_t [8]));
	can2_recv_queue_eiu5_b = xQueueCreate(1, sizeof(uint8_t [8]));
	can2_recv_queue_eiu6_b = xQueueCreate(1, sizeof(uint8_t [8]));
	can2_recv_queue_bpcm61_b = xQueueCreate(1, sizeof(uint8_t [8]));
	can2_recv_queue_bpcm62_b = xQueueCreate(1, sizeof(uint8_t [8]));
	can2_recv_queue_bpcm63_b = xQueueCreate(1, sizeof(uint8_t [8]));
	can2_recv_queue_bccm71_b = xQueueCreate(1, sizeof(uint8_t [8]));
	can2_recv_queue_bccm72_b = xQueueCreate(1, sizeof(uint8_t [8]));
	can2_recv_queue_bccm73_b = xQueueCreate(1, sizeof(uint8_t [8]));
	can2_recv_queue_bccm74_b = xQueueCreate(1, sizeof(uint8_t [8]));
	can2_recv_queue_ibcm81_b = xQueueCreate(1, sizeof(uint8_t [8]));
	can2_recv_queue_ibcm82_b = xQueueCreate(1, sizeof(uint8_t [8]));
	can2_send_queue_91 = xQueueCreate(1, sizeof(uint8_t [8]));
	can2_send_queue_92 = xQueueCreate(1, sizeof(uint8_t [8]));

	mvb_recv_queue_421 = xQueueCreate(1, sizeof(uint16_t [16]));
	mvb_recv_queue_021 = xQueueCreate(1, sizeof(uint16_t [16]));
	mvb_recv_queue_703 = xQueueCreate(1, sizeof(uint16_t [16]));
	mvb_recv_queue_707 = xQueueCreate(1, sizeof(uint16_t [16]));
	mvb_send_queue_137 = xQueueCreate(1, sizeof(uint16_t [16]));
	mvb_send_queue_702 = xQueueCreate(1, sizeof(uint16_t [16]));
	mvb_send_queue_706 = xQueueCreate(1, sizeof(uint16_t [16]));
}
void CanRecvQueueLoop()
{
	BaseType_t flag = pdFALSE;
	flag = xQueueReceive(can1_recv_queue_ebv11_a, EBV11_A, 0);
	if(flag == pdTRUE)
		EBV_Can1_cnt = 0;
	flag = xQueueReceive(can1_recv_queue_eiu31_a, EIU31_A, 0);
	if(flag == pdTRUE)
		EIU_Can1_cnt = 0;
	flag = xQueueReceive(can1_recv_queue_eiu32_a, EIU32_A, 0);
	if(flag == pdTRUE)
		EIU_Can1_cnt = 0;
	flag = xQueueReceive(can1_recv_queue_eiu33_a, EIU33_A, 0);
	if(flag == pdTRUE)
		EIU_Can1_cnt = 0;
	flag = xQueueReceive(can1_recv_queue_eiu3a_a, EIU3A_A, 0);
	if(flag == pdTRUE)
		EIU_Can1_cnt = 0;
	xQueueReceive(can1_recv_queue_eiu3_a, EIUA3_A, 0);
	xQueueReceive(can1_recv_queue_eiu4_a, EIUA4_A, 0);
	xQueueReceive(can1_recv_queue_eiu5_a, EIUA5_A, 0);
	xQueueReceive(can1_recv_queue_eiu6_a, EIUA6_A, 0);
	xQueueReceive(can1_recv_queue_bpcm61_a, BPCM61_A, 0);
	if(flag == pdTRUE)
		BPCM_Can1_cnt = 0;
	flag = xQueueReceive(can1_recv_queue_bpcm62_a, BPCM62_A, 0);
	if(flag == pdTRUE)
		BPCM_Can1_cnt = 0;
	flag = xQueueReceive(can1_recv_queue_bpcm63_a, BPCM63_A, 0);
	if(flag == pdTRUE)
		BPCM_Can1_cnt = 0;
	flag = xQueueReceive(can1_recv_queue_bccm71_a, BCCM71_A, 0);
	if(flag == pdTRUE)
		BCCM_Can1_cnt = 0;
	flag = xQueueReceive(can1_recv_queue_bccm72_a, BCCM72_A, 0);
	if(flag == pdTRUE)
		BCCM_Can1_cnt = 0;
	flag = xQueueReceive(can1_recv_queue_bccm73_a, BCCM73_A, 0);
	if(flag == pdTRUE)
		BCCM_Can1_cnt = 0;
	flag = xQueueReceive(can1_recv_queue_bccm74_a, BCCM74_A, 0);
	if(flag == pdTRUE)
		BCCM_Can1_cnt = 0;
	flag = xQueueReceive(can1_recv_queue_ibcm81_a, IBCM81_A, 0);
	if(flag == pdTRUE)
		IBCM_Can1_cnt = 0;
	flag = xQueueReceive(can1_recv_queue_ibcm82_a, IBCM82_A, 0);
	if(flag == pdTRUE)
		IBCM_Can1_cnt = 0;

	flag = xQueueReceive(can2_recv_queue_ebv11_b, EBV11_A, 0);
	if(flag == pdTRUE)
		EBV_Can2_cnt = 0;
	flag = xQueueReceive(can2_recv_queue_eiu31_b, EIU31_A, 0);
	if(flag == pdTRUE)
		EIU_Can2_cnt = 0;
	flag = xQueueReceive(can2_recv_queue_eiu32_b, EIU32_A, 0);
	if(flag == pdTRUE)
		EIU_Can2_cnt = 0;
	flag = xQueueReceive(can2_recv_queue_eiu33_b, EIU33_A, 0);
	if(flag == pdTRUE)
		EIU_Can2_cnt = 0;
	flag = xQueueReceive(can2_recv_queue_eiu3a_b, EIU3A_A, 0);
	if(flag == pdTRUE)
		EIU_Can2_cnt = 0;
	xQueueReceive(can2_recv_queue_eiu3_b, EIUA3_A, 0);
	xQueueReceive(can2_recv_queue_eiu4_b, EIUA4_A, 0);
	xQueueReceive(can2_recv_queue_eiu5_b, EIUA5_A, 0);
	xQueueReceive(can2_recv_queue_eiu6_b, EIUA6_A, 0);
	flag = xQueueReceive(can2_recv_queue_bpcm61_b, BPCM61_A, 0);
	if(flag == pdTRUE)
		BPCM_Can2_cnt = 0;
	flag = xQueueReceive(can2_recv_queue_bpcm62_b, BPCM62_A, 0);
	if(flag == pdTRUE)
		BPCM_Can2_cnt = 0;
	flag = xQueueReceive(can2_recv_queue_bpcm63_b, BPCM63_A, 0);
	if(flag == pdTRUE)
		BPCM_Can2_cnt = 0;
	flag = xQueueReceive(can2_recv_queue_bccm71_b, BCCM71_A, 0);
	if(flag == pdTRUE)
		BCCM_Can2_cnt = 0;
	flag = xQueueReceive(can2_recv_queue_bccm72_b, BCCM72_A, 0);
	if(flag == pdTRUE)
		BCCM_Can2_cnt = 0;
	flag = xQueueReceive(can2_recv_queue_bccm73_b, BCCM73_A, 0);
	if(flag == pdTRUE)
		BCCM_Can2_cnt = 0;
	flag = xQueueReceive(can2_recv_queue_bccm74_b, BCCM74_A, 0);
	if(flag == pdTRUE)
		BCCM_Can2_cnt = 0;
	flag = xQueueReceive(can2_recv_queue_ibcm81_b, IBCM81_A, 0);
	if(flag == pdTRUE)
		IBCM_Can2_cnt = 0;
	flag = xQueueReceive(can2_recv_queue_ibcm82_b, IBCM82_A, 0);
	if(flag == pdTRUE)
		IBCM_Can2_cnt = 0;
}
void RecordInit()
{
    pCANID31 = (uint8_t *)recordWord;
    pCANID32 = (uint8_t *)(&recordWord[4]);
    pCANID33 = (uint8_t *)(&recordWord[8]);
    pCANID34 = (uint8_t *)(&recordWord[12]);
    pCANID3A = (uint8_t *)(&recordWord[16]);
    pCANID61 = (uint8_t *)(&recordWord[20]);
    pCANID62 = (uint8_t *)(&recordWord[24]);
    pCANID63 = (uint8_t *)(&recordWord[28]);
    pCANID64 = (uint8_t *)(&recordWord[32]);
    pCANID71 = (uint8_t *)(&recordWord[36]);
    pCANID72 = (uint8_t *)(&recordWord[40]);
    pCANID73 = (uint8_t *)(&recordWord[44]);
    pCANID74 = (uint8_t *)(&recordWord[48]);
    pCANID81 = (uint8_t *)(&recordWord[52]);
    pCANID82 = (uint8_t *)(&recordWord[56]);
    pCANID83 = (uint8_t *)(&recordWord[60]);
    pCANID1A = (uint8_t *)(&recordWord[64]);
    pCANID7A = (uint8_t *)(&recordWord[68]);
    pCANID91 = (uint8_t *)(&recordWord[72]);
    pCANID92 = (uint8_t *)(&recordWord[76]);
    pMVB137  = &recordWord[80];
    pMVB421  = &recordWord[96];
    pMVB21   = &recordWord[112];
    pMVB703  = &recordWord[128];
    pMVB707  = &recordWord[144];
    pMVB702  = &recordWord[160];
    pMVB706  = &recordWord[176];
/**20230613_begin*************************************************************/
    pYearMonth = (uint8_t *)(&recordWord[251]);
    pDayHour = (uint8_t *)(&recordWord[252]);
    pMinuteSeconds = (uint8_t *)(&recordWord[253]);
    pMicroS = (uint32_t *)(&recordWord[254]);
}
void QosSocketInit()
{
	int x=0;
	enet_qos_sock=socket(AF_INET,SOCK_DGRAM,0);
	enet_qos_cli.sin_addr.s_addr=enet_qos_netif_ipaddr.addr;
	enet_qos_cli.sin_family=AF_INET;
	enet_qos_cli.sin_port=htons(17224);
	enet_qos_cli.sin_len=sizeof(enet_qos_cli);
	bind(enet_qos_sock,(struct sockaddr *)&enet_qos_cli,sizeof(struct sockaddr));

	enet_qos_mreq.imr_interface.s_addr=enet_qos_netif_ipaddr.addr;
	enet_qos_mreq.imr_multiaddr.s_addr=inet_addr(RECV0MULTIADDR);
	x=setsockopt(enet_qos_sock,IPPROTO_IP,IP_ADD_MEMBERSHIP,&enet_qos_mreq,sizeof(struct ip_mreq));
	printf("qosmul0:%d\n",x);
}
void TaskCreat()
{
	BaseType_t x;
	RecordInit();
	Can1Init(250000);
	Can2Init(250000);
	QosSocketInit();
	RtcInit();
	MvbInit();
    SEMC->IOCR |= 0x00908BB6;	// A8:CE#0, CSX0:A24, CSX1:A25, CSX2:CE#1, RDY:CE#2
//    PRINTF("CCM_OBS_SEMC_CLK_ROOT：  %d\r\n", CLOCK_GetFreqFromObs(CCM_OBS_SEMC_CLK_ROOT));
    writeConfig();
	*(__IO uint16_t *)(PI_BASE_ADDR+0x8000) = 0xF55F;
    SEMC->INTR |= 0x3F;
    SEMC->MCR  |= 0x1;
    asm(" dsb");
    while(SEMC->MCR & 1);
//    writeMasterFrameTable();
	USB_HostApplicationInit();
	QueueCreat();
	can_recv_event_handle = xEventGroupCreate();
	file_mutex_handle = xSemaphoreCreateMutex();
	record_data_mutex_handle = xSemaphoreCreateMutex();

	x = xTaskCreate((TaskFunction_t )MainTask,
					(const char*    )"MainTask",
					(uint16_t       )MainTask_STK_SIZE,
					(void*          )NULL,
					(UBaseType_t    )MainTask_PRIO,
					(TaskHandle_t*  )&MainTask_Handler);
	x = xTaskCreate((TaskFunction_t )Can1SendTask,
					(const char*    )"Can1SendTask",
					(uint16_t       )Can1SendTask_STK_SIZE,
					(void*          )NULL,
					(UBaseType_t    )Can1SendTask_PRIO,
					(TaskHandle_t*  )&Can1SendTask_Handler);
	printf("Can1SendTask:%d\n", x);
	x = xTaskCreate((TaskFunction_t )Can2SendTask,
					(const char*    )"Can2SendTask",
					(uint16_t       )Can2SendTask_STK_SIZE,
					(void*          )NULL,
					(UBaseType_t    )Can2SendTask_PRIO,
					(TaskHandle_t*  )&Can2SendTask_Handler);
	printf("Can2SendTask:%d\n", x);
	x = xTaskCreate((TaskFunction_t )MvbSendTask,
					(const char*    )"MvbSendTask",
					(uint16_t       )MvbSendTask_STK_SIZE,
					(void*          )NULL,
					(UBaseType_t    )MvbSendTask_PRIO,
					(TaskHandle_t*  )&MvbSendTask_Handler);
	printf("MVBSendTask:%d\n", x);
	x = xTaskCreate((TaskFunction_t )MvbRecvTask,
					(const char*    )"MvbRecvTask",
					(uint16_t       )MvbRecvTask_STK_SIZE,
					(void*          )NULL,
					(UBaseType_t    )MvbRecvTask_PRIO,
					(TaskHandle_t*  )&MvbRecvTask_Handler);
	printf("MvbRecvTask:%d\n", x);
	x = xTaskCreate((TaskFunction_t )SdCardTask,
					(const char*    )"SdCardTask",
					(uint16_t       )SdCardTask_STK_SIZE,
					(void*          )NULL,
					(UBaseType_t    )SdCardTask_PRIO,
					(TaskHandle_t*  )&SdCardTask_Handler);
	printf("SdCardTask:%d\n", x);
	x = xTaskCreate((TaskFunction_t )FtpTask,
					(const char*    )"FtpTask",
					(uint16_t       )FtpTask_STK_SIZE,
					(void*          )NULL,
					(UBaseType_t    )FtpTask_PRIO,
					(TaskHandle_t*  )&FtpTask_Handler);
	printf("FtpTask:%d\n", x);
	x = xTaskCreate((TaskFunction_t )EthQosRecvTask,
					(const char*    )"EthQosRecvTask",
					(uint16_t       )EthQosRecvTask_STK_SIZE,
					(void*          )NULL,
					(UBaseType_t    )EthQosRecvTask_PRIO,
					(TaskHandle_t*  )&EthQosRecvTask_Handler);
	printf("EthQosRecvTask:%d\n", x);
	x = xTaskCreate((TaskFunction_t )EthQosSendTask,
					(const char*    )"EthQosSendTask",
					(uint16_t       )EthQosSendTask_STK_SIZE,
					(void*          )NULL,
					(UBaseType_t    )EthQosSendTask_PRIO,
					(TaskHandle_t*  )&EthQosSendTask_Handler);
	printf("EthQosSendTask:%d\n", x);
	x = xTaskCreate((TaskFunction_t )LedTask,
					(const char*    )"LedTask",
					(uint16_t       )LedTask_STK_SIZE,
					(void*          )NULL,
					(UBaseType_t    )LedTask_PRIO,
					(TaskHandle_t*  )&LedTask_Handler);
	printf("LedTask:%d\n", x);
}
void MainTask(void *pvParameters)
{
	for(;;)
	{
		CanRecvQueueLoop();
		UserLoop();
		xQueueOverwrite(mvb_send_queue_137, MVB137);
		xQueueOverwrite(mvb_send_queue_702, MVB702);
		xQueueOverwrite(mvb_send_queue_706, MVB706);
		xQueueOverwrite(can1_send_queue_91, CAN91);
		xQueueOverwrite(can2_send_queue_91, CAN91);
		xQueueOverwrite(can1_send_queue_92, CAN92);
		xQueueOverwrite(can2_send_queue_92, CAN92);

		/**20230613_end*************************************************************/
		for(uint32_t i = 0; i < 8; i++)
		{
			pCANID31[i] = EIU31[i];
			pCANID32[i] = EIU32[i];
			pCANID33[i] = EIU33[i];
//				pCANID34[i] = EIU34[i];
			pCANID3A[i] = EIU3A[i];
			pCANID61[i] = BPCM61[i];
			pCANID62[i] = BPCM62[i];
			pCANID63[i] = BPCM63[i];
//				pCANID64[i] = [i];
			pCANID71[i] = BCCM71[i];
			pCANID72[i] = BCCM72[i];
			pCANID73[i] = BCCM73[i];
			pCANID74[i] = BCCM74[i];
			pCANID81[i] = IBCM81[i];
			pCANID82[i] = IBCM82[i];
//				pCANID83[i] = [i];
			pCANID1A[i] = EBV11[i];
//				pCANID7A[i] = [i];
			pCANID91[i] = CAN91[i];
			pCANID92[i] = CAN92[i];
		}
		for(uint32_t i = 0; i < 16; i++)
		{
			pMVB137[i] = MVB137[i];
			pMVB421[i] = MVB421[i];
			pMVB21[i]  = MVB021[i];
			pMVB703[i] = MVB703[i];
			pMVB707[i] = MVB707[i];
			pMVB702[i] = MVB702[i];
			pMVB706[i] = MVB706[i];
		}
		xSemaphoreTake(record_data_mutex_handle, portMAX_DELAY);
		memcpy(record_buff, recordWord, sizeof(record_buff));
		xSemaphoreGive(record_data_mutex_handle);
		vTaskDelay(50);
	}
}
void Can1SendTask(void *pvParameters)
{
	flexcan_frame_t can1_send_frame91 =
	{
		can1_send_frame91.id     = FLEXCAN_ID_STD(0x91),
		can1_send_frame91.format = (uint8_t)kFLEXCAN_FrameFormatStandard,
		can1_send_frame91.type   = (uint8_t)kFLEXCAN_FrameTypeData,
		can1_send_frame91.length = 8,
	};
	flexcan_frame_t can1_send_frame92 =
	{
		can1_send_frame92.id     = FLEXCAN_ID_STD(0x92),
		can1_send_frame92.type   = (uint8_t)kFLEXCAN_FrameTypeData,
		can1_send_frame92.format = (uint8_t)kFLEXCAN_FrameFormatStandard,
		can1_send_frame92.length = 8,
	};
	uint8_t can1_send_data91[8] = {0}, can1_send_data92[8] = {0};
	for(;;)
	{
		xQueueReceive(can1_send_queue_91, can1_send_data91, 0);
		can1_send_frame91.dataByte0 = can1_send_data91[0];
		can1_send_frame91.dataByte1 = can1_send_data91[1];
		can1_send_frame91.dataByte2 = can1_send_data91[2];
		can1_send_frame91.dataByte3 = can1_send_data91[3];
		can1_send_frame91.dataByte4 = can1_send_data91[4];
		can1_send_frame91.dataByte5 = can1_send_data91[5];
		can1_send_frame91.dataByte6 = can1_send_data91[6];
		can1_send_frame91.dataByte7 = can1_send_data91[7];
		Can1Send(can1_send_frame91);

		xQueueReceive(can1_send_queue_92, can1_send_data92, 0);
		can1_send_frame92.dataByte0 = can1_send_data92[0];
		can1_send_frame92.dataByte1 = can1_send_data92[1];
		can1_send_frame92.dataByte2 = can1_send_data92[2];
		can1_send_frame92.dataByte3 = can1_send_data92[3];
		can1_send_frame92.dataByte4 = can1_send_data92[4];
		can1_send_frame92.dataByte5 = can1_send_data92[5];
		can1_send_frame92.dataByte6 = can1_send_data92[6];
		can1_send_frame92.dataByte7 = can1_send_data92[7];
		Can1Send(can1_send_frame92);
		vTaskDelay(64);
	}
}
void Can2SendTask(void *pvParameters)
{
	flexcan_frame_t can2_send_frame91 =
	{
		can2_send_frame91.id     = FLEXCAN_ID_STD(0x91),
		can2_send_frame91.format = (uint8_t)kFLEXCAN_FrameFormatStandard,
		can2_send_frame91.type   = (uint8_t)kFLEXCAN_FrameTypeData,
		can2_send_frame91.length = 8,
	};
	flexcan_frame_t can2_send_frame92 =
	{
		can2_send_frame92.id     = FLEXCAN_ID_STD(0x92),
		can2_send_frame92.format = (uint8_t)kFLEXCAN_FrameFormatStandard,
		can2_send_frame92.type   = (uint8_t)kFLEXCAN_FrameTypeData,
		can2_send_frame92.length = 8,
	};
	uint8_t can2_send_data91[8] = {0}, can2_send_data92[8] = {0};
	for(;;)
	{
		xQueueReceive(can2_send_queue_91, can2_send_data91, 0);
		can2_send_frame91.dataByte0 = can2_send_data91[0];
		can2_send_frame91.dataByte1 = can2_send_data91[1];
		can2_send_frame91.dataByte2 = can2_send_data91[2];
		can2_send_frame91.dataByte3 = can2_send_data91[3];
		can2_send_frame91.dataByte4 = can2_send_data91[4];
		can2_send_frame91.dataByte5 = can2_send_data91[5];
		can2_send_frame91.dataByte6 = can2_send_data91[6];
		can2_send_frame91.dataByte7 = can2_send_data91[7];
		Can2Send(can2_send_frame91);

		xQueueReceive(can2_send_queue_92, can2_send_data92, 0);
		can2_send_frame92.dataByte0 = can2_send_data92[0];
		can2_send_frame92.dataByte1 = can2_send_data92[1];
		can2_send_frame92.dataByte2 = can2_send_data92[2];
		can2_send_frame92.dataByte3 = can2_send_data92[3];
		can2_send_frame92.dataByte4 = can2_send_data92[4];
		can2_send_frame92.dataByte5 = can2_send_data92[5];
		can2_send_frame92.dataByte6 = can2_send_data92[6];
		can2_send_frame92.dataByte7 = can2_send_data92[7];
		Can2Send(can2_send_frame92);
		vTaskDelay(64);
	}
}
void MvbSendTask(void *pvParameters)
{
	for(;;)
	{
		xQueueReceive(mvb_send_queue_137, mvb_send_137, 0);
		xQueueReceive(mvb_send_queue_702, mvb_send_702, 0);
		xQueueReceive(mvb_send_queue_706, mvb_send_706, 0);
		taskENTER_CRITICAL();
		MvbSend();
		taskEXIT_CRITICAL();
//		printf("life : %x\n",mvb_send_137[0]);
		vTaskDelay(50);
	}
}
void MvbRecvTask(void *pvParameters)
{
	for(;;)
	{
		taskENTER_CRITICAL();
		MvbRecv();
		taskEXIT_CRITICAL();
		CRCBYTE421[0] = M_MVB421[0]&0x00ff;
		CRCBYTE421[1] = (M_MVB421[0]&0xff00)>>8;
		CRCBYTE421[2] = M_MVB421[1]&0x00ff;
		CRCBYTE421[3] = (M_MVB421[1]&0xff00)>>8;
		CRCBYTE421[4] = M_MVB421[2]&0x00ff;
		CRCBYTE421[5] = (M_MVB421[2]&0xff00)>>8;
		CRCBYTE421[6] = M_MVB421[3]&0x00ff;
		CRCBYTE421[7] = (M_MVB421[3]&0xff00)>>8;
		CRCBYTE421[8] = M_MVB421[4]&0x00ff;
		CRCBYTE421[9] = (M_MVB421[4]&0xff00)>>8;
		CRCBYTE421[10] = M_MVB421[5]&0x00ff;
		CRCBYTE421[11] = (M_MVB421[5]&0xff00)>>8;
		CRCBYTE421[12] = M_MVB421[6]&0x00ff;
		CRCBYTE421[13] = (M_MVB421[6]&0xff00)>>8;
		CRCBYTE421[14] = M_MVB421[7]&0x00ff;
		CRCBYTE421[15] = (M_MVB421[7]&0xff00)>>8;
		CRCBYTE421[16] = M_MVB421[8]&0x00ff;
		CRCBYTE421[17] = (M_MVB421[8]&0xff00)>>8;
		CRCBYTE421[18] = M_MVB421[9]&0x00ff;
		CRCBYTE421[19] = (M_MVB421[9]&0xff00)>>8;
		CRCBYTE421[20] = M_MVB421[10]&0x00ff;
		CRCBYTE421[21] = (M_MVB421[10]&0xff00)>>8;
		CRCBYTE421[22] = M_MVB421[11]&0x00ff;
		CRCBYTE421[23] = (M_MVB421[11]&0xff00)>>8;
		CRCBYTE421[24] = M_MVB421[12]&0x00ff;
		CRCBYTE421[25] = (M_MVB421[12]&0xff00)>>8;
		CRCBYTE421[26] = M_MVB421[13]&0x00ff;
		CRCBYTE421[27] = (M_MVB421[13]&0xff00)>>8;
		CRCBYTE421[28] = M_MVB421[14]&0x00ff;
		CRCBYTE421[29] = (M_MVB421[14]&0xff00)>>8;

		CRC_MVB421 = Comn_F_GetCRC16( c_Crc16Table_aU16,0xffff,CRCBYTE421,30);
		CRC_MVB421 = ((CRC_MVB421&0x00ff)<<8)| ((CRC_MVB421&0xff00)>>8);

		if(CRC_MVB421 != M_MVB421[15])
			CRC_MVB421_err = 1;
		else
		{
			CRC_MVB421_err = 0;
			xQueueOverwrite(mvb_recv_queue_421, M_MVB421);
		}

		CRCBYTE021[0] = M_MVB021[0]&0x00ff;
		CRCBYTE021[1] = (M_MVB021[0]&0xff00)>>8;
		CRCBYTE021[2] = M_MVB021[1]&0x00ff;
		CRCBYTE021[3] = (M_MVB021[1]&0xff00)>>8;
		CRCBYTE021[4] = M_MVB021[2]&0x00ff;
		CRCBYTE021[5] = (M_MVB021[2]&0xff00)>>8;
		CRCBYTE021[6] = M_MVB021[3]&0x00ff;
		CRCBYTE021[7] = (M_MVB021[3]&0xff00)>>8;
		CRCBYTE021[8] = M_MVB021[4]&0x00ff;
		CRCBYTE021[9] = (M_MVB021[4]&0xff00)>>8;
		CRCBYTE021[10] = M_MVB021[5]&0x00ff;
		CRCBYTE021[11] = (M_MVB021[5]&0xff00)>>8;
		CRCBYTE021[12] = M_MVB021[6]&0x00ff;
		CRCBYTE021[13] = (M_MVB021[6]&0xff00)>>8;
		CRCBYTE021[14] = M_MVB021[7]&0x00ff;
		CRCBYTE021[15] = (M_MVB021[7]&0xff00)>>8;
		CRCBYTE021[16] = M_MVB021[8]&0x00ff;
		CRCBYTE021[17] = (M_MVB021[8]&0xff00)>>8;
		CRCBYTE021[18] = M_MVB021[9]&0x00ff;
		CRCBYTE021[19] = (M_MVB021[9]&0xff00)>>8;
		CRCBYTE021[20] = M_MVB021[10]&0x00ff;
		CRCBYTE021[21] = (M_MVB021[10]&0xff00)>>8;
		CRCBYTE021[22] = M_MVB021[11]&0x00ff;
		CRCBYTE021[23] = (M_MVB021[11]&0xff00)>>8;
		CRCBYTE021[24] = M_MVB021[12]&0x00ff;
		CRCBYTE021[25] = (M_MVB021[12]&0xff00)>>8;
		CRCBYTE021[26] = M_MVB021[13]&0x00ff;
		CRCBYTE021[27] = (M_MVB021[13]&0xff00)>>8;
		CRCBYTE021[28] = M_MVB021[14]&0x00ff;
		CRCBYTE021[29] = (M_MVB021[14]&0xff00)>>8;

		CRC_MVB021 = Comn_F_GetCRC16( c_Crc16Table_aU16,0xffff,CRCBYTE021,30);
		CRC_MVB021 = ((CRC_MVB021&0x00ff)<<8)| ((CRC_MVB021&0xff00)>>8);

		if(CRC_MVB021 != M_MVB021[15])
			CRC_MVB021_err = 1;
		else
		{
			CRC_MVB021_err = 0;
			xQueueOverwrite(mvb_recv_queue_021, M_MVB021);
		}

		CRCBYTE703[0] = M_MVB703[0]&0x00ff;
		CRCBYTE703[1] = (M_MVB703[0]&0xff00)>>8;
		CRCBYTE703[2] = M_MVB703[1]&0x00ff;
		CRCBYTE703[3] = (M_MVB703[1]&0xff00)>>8;
		CRCBYTE703[4] = M_MVB703[2]&0x00ff;
		CRCBYTE703[5] = (M_MVB703[2]&0xff00)>>8;
		CRCBYTE703[6] = M_MVB703[3]&0x00ff;
		CRCBYTE703[7] = (M_MVB703[3]&0xff00)>>8;
		CRCBYTE703[8] = M_MVB703[4]&0x00ff;
		CRCBYTE703[9] = (M_MVB703[4]&0xff00)>>8;
		CRCBYTE703[10] = M_MVB703[5]&0x00ff;
		CRCBYTE703[11] = (M_MVB703[5]&0xff00)>>8;
		CRCBYTE703[12] = M_MVB703[6]&0x00ff;
		CRCBYTE703[13] = (M_MVB703[6]&0xff00)>>8;
		CRCBYTE703[14] = M_MVB703[7]&0x00ff;
		CRCBYTE703[15] = (M_MVB703[7]&0xff00)>>8;
		CRCBYTE703[16] = M_MVB703[8]&0x00ff;
		CRCBYTE703[17] = (M_MVB703[8]&0xff00)>>8;
		CRCBYTE703[18] = M_MVB703[9]&0x00ff;
		CRCBYTE703[19] = (M_MVB703[9]&0xff00)>>8;
		CRCBYTE703[20] = M_MVB703[10]&0x00ff;
		CRCBYTE703[21] = (M_MVB703[10]&0xff00)>>8;
		CRCBYTE703[22] = M_MVB703[11]&0x00ff;
		CRCBYTE703[23] = (M_MVB703[11]&0xff00)>>8;
		CRCBYTE703[24] = M_MVB703[12]&0x00ff;
		CRCBYTE703[25] = (M_MVB703[12]&0xff00)>>8;
		CRCBYTE703[26] = M_MVB703[13]&0x00ff;
		CRCBYTE703[27] = (M_MVB703[13]&0xff00)>>8;
		CRCBYTE703[28] = M_MVB703[14]&0x00ff;
		CRCBYTE703[29] = (M_MVB703[14]&0xff00)>>8;

		CRC_MVB703 = Comn_F_GetCRC16( c_Crc16Table_aU16,0xffff,CRCBYTE703,30);
		CRC_MVB703 = ((CRC_MVB703&0x00ff)<<8)| ((CRC_MVB703&0xff00)>>8);

		if(CRC_MVB703 != M_MVB703[15])
			CRC_MVB703_err = 1;
		else
		{
			CRC_MVB703_err = 0;
			xQueueOverwrite(mvb_recv_queue_703, M_MVB703);
		}

		CRCBYTE707[0] = M_MVB707[0]&0x00ff;
		CRCBYTE707[1] = (M_MVB707[0]&0xff00)>>8;
		CRCBYTE707[2] = M_MVB707[1]&0x00ff;
		CRCBYTE707[3] = (M_MVB707[1]&0xff00)>>8;
		CRCBYTE707[4] = M_MVB707[2]&0x00ff;
		CRCBYTE707[5] = (M_MVB707[2]&0xff00)>>8;
		CRCBYTE707[6] = M_MVB707[3]&0x00ff;
		CRCBYTE707[7] = (M_MVB707[3]&0xff00)>>8;
		CRCBYTE707[8] = M_MVB707[4]&0x00ff;
		CRCBYTE707[9] = (M_MVB707[4]&0xff00)>>8;
		CRCBYTE707[10] = M_MVB707[5]&0x00ff;
		CRCBYTE707[11] = (M_MVB707[5]&0xff00)>>8;
		CRCBYTE707[12] = M_MVB707[6]&0x00ff;
		CRCBYTE707[13] = (M_MVB707[6]&0xff00)>>8;
		CRCBYTE707[14] = M_MVB707[7]&0x00ff;
		CRCBYTE707[15] = (M_MVB707[7]&0xff00)>>8;
		CRCBYTE707[16] = M_MVB707[8]&0x00ff;
		CRCBYTE707[17] = (M_MVB707[8]&0xff00)>>8;
		CRCBYTE707[18] = M_MVB707[9]&0x00ff;
		CRCBYTE707[19] = (M_MVB707[9]&0xff00)>>8;
		CRCBYTE707[20] = M_MVB707[10]&0x00ff;
		CRCBYTE707[21] = (M_MVB707[10]&0xff00)>>8;
		CRCBYTE707[22] = M_MVB707[11]&0x00ff;
		CRCBYTE707[23] = (M_MVB707[11]&0xff00)>>8;
		CRCBYTE707[24] = M_MVB707[12]&0x00ff;
		CRCBYTE707[25] = (M_MVB707[12]&0xff00)>>8;
		CRCBYTE707[26] = M_MVB707[13]&0x00ff;
		CRCBYTE707[27] = (M_MVB707[13]&0xff00)>>8;
		CRCBYTE707[28] = M_MVB707[14]&0x00ff;
		CRCBYTE707[29] = (M_MVB707[14]&0xff00)>>8;

		CRC_MVB707 = Comn_F_GetCRC16( c_Crc16Table_aU16,0xffff,CRCBYTE707,30);
		CRC_MVB707 = ((CRC_MVB707&0x00ff)<<8)| ((CRC_MVB707&0xff00)>>8);

		if(CRC_MVB707 != M_MVB707[15])
			CRC_MVB707_err = 1;
		else
		{
			CRC_MVB707_err = 0;
			xQueueOverwrite(mvb_recv_queue_707, M_MVB707);
		}
		vTaskDelay(50);
	}
}
void SdCardTask(void *pvParameters)
{
	uint16_t record_data[256] = {0};
	SdInit();
	for(;;)
	{
		xSemaphoreTake(record_data_mutex_handle, portMAX_DELAY);
		memcpy(record_data, record_buff, sizeof(record_data));
		xSemaphoreGive(record_data_mutex_handle);
		SNVS_LP_SRTC_GetDatetime(SNVS, &rtc_date_now);
		record_data[251] = rtc_date_now.year-2000;
		record_data[251] |= rtc_date_now.month << 8;
		record_data[252] = rtc_date_now.day;
		record_data[252] |= rtc_date_now.hour << 8;;
		record_data[253] = rtc_date_now.minute;
		record_data[253] |= rtc_date_now.second << 8;;
		xSemaphoreTake(file_mutex_handle, portMAX_DELAY);
		SdRecordLoop(record_data, sizeof(record_data));
		xSemaphoreGive(file_mutex_handle);
		vTaskDelay(200);
	}
}
void FtpTask(void *pvParameters)
{
	FtpInit();
	for(;;)
	{
		FtpLoop();
	}
}
uint8_t CrcAdd(uint8_t * data, uint32_t len)
{
	uint8_t crc = 0;
	for(uint32_t i = 0; i < len; i++)
	{
		crc += data[i];
	}
	return crc;
}
void EthQosRecvTask(void *pvParameters)
{
	snvs_lp_srtc_datetime_t set_date = {0};
	for(;;)
	{
		int recv_len = recvfrom(enet_qos_sock, (uint8_t *)&enet_qos_recv_buff, sizeof(enet_qos_recv_buff), 0, NULL, NULL);
		if(recv_len > 0)
		{
			uint8_t device = enet_qos_recv_buff[0];
			uint8_t type = enet_qos_recv_buff[1];
			uint16_t length = (enet_qos_recv_buff[2] << 8) | enet_qos_recv_buff[3];
			uint8_t crc = CrcAdd(enet_qos_recv_buff, recv_len - 1);
			if(crc == enet_qos_recv_buff[recv_len - 1])
			{

			}
			if(device == 0x1)
			{
				if(type == 0x1)
				{
					set_date.year = (enet_qos_recv_buff[4] << 8) | enet_qos_recv_buff[5];
					set_date.month = enet_qos_recv_buff[6];
					set_date.day = enet_qos_recv_buff[7];
					set_date.hour = enet_qos_recv_buff[8];
					set_date.minute = enet_qos_recv_buff[9];
					set_date.second = enet_qos_recv_buff[10];
					SNVS_LP_SRTC_SetDatetime(SNVS, &set_date);
				}
			}
		}
		vTaskDelay(1);
	}
}
void EthQosSendTask(void *pvParameters)
{
	struct sockaddr_in ser={0};
	ser.sin_addr.s_addr=inet_addr(ENETQOSSENDMULTIADDR0);
	ser.sin_family=AF_INET;
	ser.sin_len=sizeof(ser);
	ser.sin_port=htons(17224);
	for(;;)
	{
		sendto(enet_qos_sock,(uint8_t *)&cur_ver,sizeof(cur_ver),0,(struct sockaddr *)&ser,sizeof(ser));
		vTaskDelay(200);
	}
}
void LedTask(void *pvParameters)
{
	EventBits_t event_bit_flag = 0;
	uint32_t can1_recv_timeout = 0;
	uint32_t can2_recv_timeout = 0;
	for(;;)
	{
		event_bit_flag = xEventGroupWaitBits((EventGroupHandle_t )can_recv_event_handle, /* 等待的事件标志组句柄 */
											(EventBits_t        )0x1,      /* 等待的事件 */
											(BaseType_t         )pdTRUE,            /* 函数退出时清零等待的事件 */
											(BaseType_t         )pdTRUE,            /* 等待等待的事件中的所有事件 */
											(TickType_t         )0);    /* 等待时间 */
		if(event_bit_flag == 0x1)
			can1_recv_timeout = 0;
		event_bit_flag = xEventGroupWaitBits((EventGroupHandle_t )can_recv_event_handle, /* 等待的事件标志组句柄 */
											(EventBits_t        )0x2,      /* 等待的事件 */
											(BaseType_t         )pdTRUE,            /* 函数退出时清零等待的事件 */
											(BaseType_t         )pdTRUE,            /* 等待等待的事件中的所有事件 */
											(TickType_t         )0);    /* 等待时间 */
		if(event_bit_flag == 0x2)
			can2_recv_timeout = 0;
		can1_recv_timeout++;
		can2_recv_timeout++;
		if(can1_recv_timeout < 40)
			GPIO_PortToggle(BOARD_INITPINS_LED_USER3_GPIO,BOARD_INITPINS_LED_USER3_GPIO_PIN_MASK);
		else
		{
			can1_recv_timeout = 40;
			GPIO_WritePinOutput(BOARD_INITPINS_LED_USER3_GPIO, BOARD_INITPINS_LED_USER3_GPIO_PIN, 0);
		}
		if(can2_recv_timeout < 40)
			GPIO_PortToggle(BOARD_INITPINS_LED_USER1_GPIO,BOARD_INITPINS_LED_USER1_GPIO_PIN_MASK);
		else
		{
			can2_recv_timeout = 40;
			GPIO_WritePinOutput(BOARD_INITPINS_LED_USER1_GPIO, BOARD_INITPINS_LED_USER1_GPIO_PIN, 0);
		}
		GPIO_PortToggle(BOARD_INITPINS_LED_RUN_GPIO,BOARD_INITPINS_LED_RUN_GPIO_PIN_MASK);
//		GPIO_PortToggle(BOARD_INITPINS_LED_USER2_GPIO,BOARD_INITPINS_LED_USER2_GPIO_PIN_MASK);
		vTaskDelay(100);
	}
}
